package com.qys.livesMall.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.shop.mapper.H2ShopHierarchyIncomeConfigMapper;
import com.qys.livesMall.shop.modal.dto.shop.income.EditHierarchyIncomeConfig;
import com.qys.livesMall.shop.modal.entity.H2ShopHierarchyIncomeConfig;
import com.qys.livesMall.shop.modal.vo.H2ShopHierarchyIncomeConfigVO;
import com.qys.livesMall.shop.service.H2ShopHierarchyIncomeConfigService;
import com.qys.livesMall.system.model.dto.sys.H2SysShareConfigDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dp
 * Date: 2023/06/06
 * Time: 11:19
 * DESCRIPTION:
 */
@Service
public class H2ShopHierarchyIncomeConfigServiceImpl extends ServiceImpl<H2ShopHierarchyIncomeConfigMapper, H2ShopHierarchyIncomeConfig> implements H2ShopHierarchyIncomeConfigService {

    /**
     * 查询层级分成比例设置
     *
     * @para
     */
    @Override
    public List<H2ShopHierarchyIncomeConfigVO> listAll() {
        List<H2ShopHierarchyIncomeConfig> shareConfigs = baseMapper.selectList(Wrappers.<H2ShopHierarchyIncomeConfig>query()
                .orderByAsc("hierarchy"));
        if (CollectionUtils.isNotEmpty(shareConfigs)) {
            return BeanUtil.copyToList(shareConfigs, H2ShopHierarchyIncomeConfigVO.class);
        }

        return new ArrayList<>();
    }

    /**
     * 新增层级配置信息
     *
     * @param config {@link  H2SysShareConfigDTO}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(EditHierarchyIncomeConfig config) {
        H2ShopHierarchyIncomeConfig sysShareConfig = new H2ShopHierarchyIncomeConfig();
        BeanUtils.copyProperties(config, sysShareConfig);
        saveOrUpdate(sysShareConfig);
    }
}
